package cn.ym.system.service.impl;

import cn.ym.basic.service.impl.BaseServiceImpl;
import cn.ym.basic.utils.AssertUtil;
import cn.ym.basic.utils.PageList;
import cn.ym.system.domain.Systemdictionarydetail;
import cn.ym.system.domain.Systemdictionarytype;
import cn.ym.system.mapper.SystemdictionarydetailMapper;
import cn.ym.system.mapper.SystemdictionarytypeMapper;
import cn.ym.system.query.SystemdictionarytypeQuery;
import cn.ym.system.service.SystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务层：数据字典类型
 */
@Service
public class SystemdictionarytypeServiceImpl extends BaseServiceImpl<Systemdictionarytype> implements SystemdictionarytypeService {

    @Autowired
    private SystemdictionarytypeMapper systemdictionarytypeMapper;
    @Autowired
    private SystemdictionarydetailMapper systemdictionarydetailMapper;
    /**
     * 添加
     * @param systemdictionarytype
     */
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void add(Systemdictionarytype systemdictionarytype) {
        AssertUtil.isNotEmpty(systemdictionarytype.getSn(),"亲：数据字典类型编号不能为空");
        AssertUtil.isNotEmpty(systemdictionarytype.getName(),"亲：数据字典类型名称不能为空");
        systemdictionarytypeMapper.add(systemdictionarytype);
    }

    /**
     * 修改
     * @param systemdictionarytype
     */
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void update(Systemdictionarytype systemdictionarytype) {
        AssertUtil.isNotEmpty(systemdictionarytype.getSn(),"亲：数据字典类型编号不能为空");
        AssertUtil.isNotEmpty(systemdictionarytype.getName(),"亲：数据字典类型名称不能为空");
        systemdictionarytypeMapper.update(systemdictionarytype);
    }

    /**
     * 删除++++++++删除级联---就是删除明细表对应的数据
     * @param id
     */
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void delete(Long id) {
        //1.先查询出所有字典明细
        List<Systemdictionarydetail> details = systemdictionarydetailMapper.findAll();
        for (Systemdictionarydetail detail : details) {
            //2.明细表的TypesId==前端点击类型表传过来的id，就删除明细
            if (detail.getTypesId()==id){
                systemdictionarydetailMapper.delete(detail.getId());
            }
        }
        super.delete(id);
    }


    /**
     * 批量删除
     * @param ids
     */
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void patchDel(Long[] ids) {
        //1.先查询出所有字典明细
        List<Systemdictionarydetail> details = systemdictionarydetailMapper.findAll();
        for (Systemdictionarydetail detail : details) {
            //2.明细表的TypesId==前端点击类型表传过来的id，就删除明细
            for (Long id : ids) {
                if (detail.getTypesId()==id){
                    systemdictionarydetailMapper.delete(detail.getId());
                }
            }
        }
        super.patchDel(ids);

    }



}
